package com.hjxr.sysadmin.zsj.service.impl;

import java.util.Date;
import java.util.List;

import com.hjxr.sysadmin.zsj.domain.ZIndustry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjxr.sysadmin.zsj.mapper.LocationMapper;
import com.hjxr.sysadmin.zsj.domain.Location;
import com.hjxr.sysadmin.zsj.service.ILocationService;

/**
 * 区域信息Service业务层处理
 *
 * @author zc
 * @date 2021-04-12
 */
@Service
public class LocationServiceImpl implements ILocationService
{
    @Autowired
    private LocationMapper locationMapper;

    /**
     * 查询区域信息
     *
     * @param locationId 区域信息ID
     * @return 区域信息
     */
    @Override
    public Location selectLocationById(Long locationId)
    {
        Location location = locationMapper.selectLocationById(locationId);
        if (location != null && location.getLocationCondition() != null){
            location.setConditionList(location.getLocationCondition().split("\\\\n"));
        }
        return location;
    }

    /**
     * 查询区域信息列表
     *
     * @param location 区域信息
     * @return 区域信息
     */
    @Override
    public List<Location> selectLocationList(Location location)
    {
        List<Location> list = locationMapper.selectLocationList(location);
        if (list != null && list.size() > 0) {
            for (Location location1 : list) {
                if (location1 != null && location1.getLocationCondition() != null){
                    location1.setLocationCondition(location1.getLocationCondition().replace("\\n","<br>"));
                }
            }
        }
        return list;
    }

    /**
     * 新增区域信息
     *
     * @param location 区域信息
     * @return 结果
     */
    @Override
    public int insertLocation(Location location)
    {
        if (location.getLocationCondition() != null) {
            String condi = "";
            for (String s : location.getConditionList()) {
                condi += (s + "\\n");
            }
            location.setLocationCondition(condi);
        }
        location.setModifiedTime(new Date());
        return locationMapper.insertLocation(location);
    }

    /**
     * 修改区域信息
     *
     * @param location 区域信息
     * @return 结果
     */
    @Override
    public int updateLocation(Location location)
    {
        if (location.getLocationCondition() != null) {
            String condi = "";
            for (String s : location.getConditionList()) {
                condi += (s + "\\n");
            }
            location.setLocationCondition(condi);
        }
        location.setModifiedTime(new Date());
        return locationMapper.updateLocation(location);
    }

    /**
     * 批量删除区域信息
     *
     * @param locationIds 需要删除的区域信息ID
     * @return 结果
     */
    @Override
    public int deleteLocationByIds(Long[] locationIds)
    {
        return locationMapper.deleteLocationByIds(locationIds);
    }

    /**
     * 删除区域信息信息
     *
     * @param locationId 区域信息ID
     * @return 结果
     */
    @Override
    public int deleteLocationById(Long locationId)
    {
        return locationMapper.deleteLocationById(locationId);
    }

    /**
     * 查询区域主导产业信息
     * @param locationName
     * @return
     */
    @Override
    public ZIndustry selectIndustryByName(String locationName) {
        ZIndustry zIndustry = locationMapper.selectIndustryByName(locationName);
        return zIndustry;
    }

    /**
     * 新增区域主导产业信息
     * @param zIndustry
     * @return
     */
    @Override
    public int insertIndustry(ZIndustry zIndustry) {
        zIndustry.setModifiedTime(new Date());
        return locationMapper.insertIndustry(zIndustry);
    }

    /**
     * 修改区域主导产业信息
     * @param zIndustry
     * @return
     */
    @Override
    public int updateIndustry(ZIndustry zIndustry) {
        zIndustry.setModifiedTime(new Date());
        return locationMapper.updateIndustry(zIndustry);
    }

    /**
     * 是否已存在当前年数据
     *
     * @param nd 年度
     * @param locationName 区域名称
     * @return 结果
     */
    @Override
    public boolean hasByNameAndYear(String nd, String locationName)
    {
        int result = locationMapper.hasByNameAndYear(nd, locationName);
        return result > 0 ? true : false;
    }
}
